package com.zt.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zt.globalExceptionHandler.BizException;
import com.zt.mapper.EmpMapper;
import com.zt.pojo.*;
import com.zt.service.EmpService;
import com.zt.utils.CurrentHolder;
import com.zt.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    //分页查询员工信息
    @Override
    public PageResult QueryPage(EmpQueryParam empQueryParam) {
            //1. 设置PageHelper分页参数
            PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
            //2. 执行查询
            List<Emp> empList = empMapper.QueryPage(empQueryParam);
            //3. 封装分页结果
            Page<Emp> p = (Page<Emp>)empList;
            return new PageResult(p.getTotal(), p.getResult());
    }

    @Transactional
    //批量删除员工信息
    @Override
    public void deleteById(Integer[] id) {
            //删除员工
            empMapper.deleteById(id);
            //删除员工工作经历
            empMapper.deleteExprById(id);
    }

    //添加员工信息
    @Override
    public void add(Emp emp) {
            //补全数据
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(emp.getCreateTime());
            emp.setPassword("123456");
            //添加员工信息
            empMapper.add(emp);
            //如果有经历就添加员工工作经历
            List<EmpExpr> empExprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(empExprList)){
                empExprList.forEach(empExpr->empExpr.setEmpId(emp.getId()));
                empMapper.addExpr(empExprList);
            }
    }

    //查询所有员工信息
    @Override
    public List<Emp> findAll() {
        List<Emp> empList = empMapper.findAll();
        return empList;
    }

    //根据id查询员工信息
    @Override
    public Emp findById(Integer id) {
        Emp emp = empMapper.findById(id);
        return emp;
    }

    //修改员工信息
    @Transactional
    @Override
    public void update(Emp emp) {
        //补全数据
        emp.setUpdateTime(LocalDateTime.now());
        //更新员工数据
        empMapper.update(emp);
        //修改员工工作经历,1:全删了
        empMapper.deleteExprById(new Integer[]{emp.getId()});
        //2:重新添加
        List<EmpExpr> empExprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(empExprList)){
            empExprList.forEach(empExpr->empExpr.setEmpId(emp.getId()));
            empMapper.addExpr(empExprList);
        }
    }

    //登录
    @Override
    public Logininfo login(Emp emp) {
        //通过用户名和密码判断是否纯在该用户
        Logininfo logininfo = empMapper.findByUserNameAndPasword(emp);
        if(logininfo == null){
            //登录失败
            throw new BizException("用户名或密码错误");
        }else {
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", logininfo.getId());
            claims.put("username", logininfo.getUsername());
            claims.put("name", logininfo.getName());
            String token = JwtUtils.generateJwt(claims);
            return logininfo.builder()
                    .id(logininfo.getId())
                    .username(logininfo.getUsername())
                    .name(logininfo.getName())
                    .token(token)
                    .build();
        }
    }

    @Override
    public void updatePassword(Password password) {
        Integer id = CurrentHolder.getCurrentId();
        Emp emp = empMapper.findById(id);
        String oldPassword = emp.getPassword();
        String newPassword = password.getNewpassword();
        if(!oldPassword.equals(password.getPassword())){
            throw new BizException("旧密码不正确");
        }
        empMapper.updatePassword(newPassword,id);
    }
}
